/********************************************************************
  Copyright by TAOBAO
  File Name: common.h
  Created Date: 2009-06-03
  Author: ALI
  Version: 0.0.1
  Last Version Date:
  Version Date:
  Description: common functions
  Modified history:
  Date      Author    Description
--------------------------------------------------------------------
  20090603  ALI      common functions(create)
********************************************************************/
#include "common.h"

static bool NotSpace(char c)
{
	return ( isspace(c) == 0 );
}

void Trim(string& strStr)
{
	LTrim(strStr);
	RTrim(strStr);
}

void LTrim(string& strStr)
{
	IterStr iterStr = find_if( strStr.begin(), strStr.end(), NotSpace);
	strStr.erase(strStr.begin(), iterStr);
}

void RTrim(string& strStr)
{
	IterStr iterStr = find_if( strStr.rbegin(), strStr.rend(), NotSpace).base();
	strStr.erase(iterStr, strStr.end());
}

string Format(const char* fmt, ... )
{
       char szStr[MAX_BUFF_SIZE];
       va_list vl;
       va_start(vl, fmt);
       vsprintf(szStr, fmt, vl);
       
       va_end(vl);
       
       return string(szStr);
}

//-----------------------------------------------------------
// ¹¦ÄÜ£º·µ»ØÎÄ¼þÀàÐÍ
// ²ÎÊý£º
//    pFileName: ÎÄ¼þÃû
// ·µ»ØÖµ£º
//    ·µ»ØÎÄ¼þÀàÐÍ±àÂë
//    1:ÆÕÍ¨ÎÄ¼þ
//    2:*.ZÎÄ¼þ
//    3:*.gzÎÄ¼þ
//    4:*.hdfs file
//-----------------------------------------------------------
int FileType( const char* pFileName )
{
       const int mNum = 2;
       size_t magic[mNum][4] = 
       {//magic¶¨Òå±í
       	//  Æ«ÒÆÁ¿ ×Ö½ÚÊý magicÖµ   ÎÄ¼þ´úÂë
       	//  {0,    0,     0,        1}, //ÆÕÍ¨ÎÄ¼þ
       	    {0,    2,     8093,     2}, //ZÎÄ¼þ
       	    {0,    4,     529205256,3}  //gzÎÄ¼þ
       };

       char szHDFS[16] = {0};
       if( sscanf(pFileName, "%4s", szHDFS) > 0 && 0 == strncmp("hdfs", szHDFS, 4))
           return 4;	// hdfs file
       
       FILE* pFile = fopen(pFileName,"r+b");
       if ( pFile == NULL )
       {//ÎÄ¼þÎÞ·¨´ò¿ª·µ»Ø-1
             return -1;
       }
       char buff[10];
       int n = -1;

       while ( ++n < mNum )
       {
       	     // ÉèÖÃÆ«ÒÆÁ¿
             fseek( pFile, magic[n][0], SEEK_SET );
             // ¶ÁÈ¡magicÖµ
             if( fread(buff,sizeof(char),magic[n][1],pFile) )
             {
                   buff[magic[n][1]] = '\0';
                   if ( GetMagic(buff) == magic[n][2] )
                   {
                         break;
                   }
             }
       }
       fclose(pFile);
       
       if ( n < mNum)
       {//Æ¥Åä³É¹¦
             return magic[n][3];
       }
       else  return 1; //ÆÕÍ¨ÎÄ¼þ
}


//-----------------------------------------------------------
// ¹¦ÄÜ£º½«×Ö·û´®×ª»»³ÉÕûÐÍ
// ²ÎÊý£º
//    pStr: ×Ö·û´®
// ·µ»ØÖµ£º
//    ÕûÐÍÖµ
//-----------------------------------------------------------
int GetMagic( const char* pStr )
{
       int value = 0;
       unsigned int tmp = 0;
       const char* pCh = pStr;
       
       int mask = 0x000000FF;

       while ( *pCh != '\0' )
       {
            tmp = *pCh;
            tmp &= mask;
            value = value << 8;
            value |= tmp;
            pCh++;
       }
       return value;
}

